package com.net.xpay.common.service.impl;

import com.google.common.base.Preconditions;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.domain.partner.PartnerUserFilings;
import com.net.xpay.common.enums.partner.PartnerUserFilingsAuditStatus;
import com.net.xpay.common.model.PartnerUserFilingsSubmitModel;
import com.net.xpay.common.validator.PartnerUserFilingsValidator;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.model.AdminPartnerUserFilingsAuditModel;
import com.net.common.annotation.WriteTransactional;
import com.net.common.exception.BusinessException;
import com.net.xpay.common.manager.partner.PartnerManager;
import com.net.xpay.common.manager.partner.PartnerUserFilingsManager;
import com.net.xpay.common.service.PartnerUserFilingsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class PartnerUserFilingsServiceImpl implements PartnerUserFilingsService {

    @Autowired
    private PartnerUserFilingsManager partnerUserFilingsManager;

    @Autowired
    private PartnerUserFilingsValidator partnerUserFilingsValidator;

    @Autowired
    private PartnerManager partnerManager;

    @Override
    public PartnerUserFilings submitPartnerUserFilings(Partner partner, PartnerUserFilingsSubmitModel model) {
        partnerUserFilingsValidator.validatePartnerUserFilings(partner, model);

        PartnerUserFilings record = new PartnerUserFilings();
        record.setPartnerId(partner.getId());
        record.setMobile(model.getMobile());
        record.setBusinessName(model.getBusinessName());
        record.setBusinessLicense(model.getBusinessLicense());
        record.setBizSceneDesc(model.getBizSceneDesc());
        record.setServiceRatio(model.getServiceRatio());
        record.setAuditStatus(PartnerUserFilingsAuditStatus.AUDITING);
        record.setAuditSubmitTime(LocalDateTime.now());

        partnerUserFilingsManager.save(record);

        return record;
    }

    @Override
    public PartnerUserFilings auditSuccess(PartnerUserFilings record) {
        record.setAuditStatus(PartnerUserFilingsAuditStatus.NORMAL);
        record.setAuditTime(LocalDateTime.now());
        partnerUserFilingsManager.update(record);
        return record;
    }

    @Override
    public void auditFail(AdminPartnerUserFilingsAuditModel model) {
        Preconditions.checkArgument(model.getId() != null);

        PartnerUserFilings record = partnerUserFilingsManager.getById(model.getId());

        if (record.getAuditStatus() != PartnerUserFilingsAuditStatus.AUDITING) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "当前状态不是待审核");
        }

        record.setAuditStatus(PartnerUserFilingsAuditStatus.FAIL);
        record.setAuditMessage(model.getAuditMessage());

        partnerUserFilingsManager.update(record);
    }
}
